/*
 * The MIT License
 *
 * Copyright 2012 Hudson.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.msservice.timeremail;

import hudson.logging.HudsonTestLog;
import hudson.model.Hudson;
import hudson.msservice.Messages;
import hudson.msservice.messageservice.MessageServiceProxy;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * this class is a timmer taks that it could send email to some duty person for
 * some job execute , summery the execution information
 * @author janksenhu
 */
public class EmailToolsExecuteInfo  implements Job{

    
    /**
     * tools' execute infor send email to duty 
     */
    public static void sendToolsExcuteInforToDuty(){
        Map<String,Map<Integer,List>> map = null;
        for(String toolname:map.keySet()){
            String to = null;
            String from = "ptadmin@tencent.com";
            String cc = "";
            String bcc = "";
            String title = Messages.Email_Tools_Title(toolname);
            String priority = MessageServiceProxy.MSG_PRIORITY_LOW;
            String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_HTML;
            String emailType = MessageServiceProxy.MSG_MAIL_TYPE_ENCHANGE;
            List<String> attachFilePath = null;
            MessageServiceProxy instance = MessageServiceProxy.getInstance();

            Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the head is "+Messages.Email_Tools_Head("http://10.6.193.204:8080/hudson"));
//            String e_head = Messages.Email_Tools_Head(Hudson.getInstance().getRootUrl());
            String e_head=Messages.Email_Tools_Head("http://10.6.193.204:8080/hudson/");
            
            if(map.get(toolname).keySet().size()==0){
                continue;
            }
            
            for(Integer buildnum : map.get(toolname).keySet()){
                List l_b=map.get(toolname).get(buildnum);
                String e_table = Messages.Email_Tools_Tables_Tr(toolname, buildnum, l_b.get(0), l_b.get(1), MessageUtil.getChineseOfResult((String)l_b.get(2)), MessageUtil.getTimeStringOfDuration((Integer)l_b.get(3)), l_b.get(4));
                e_head+=e_table;
            }
               
            String html_string = e_head+Messages.Email_Tools_Foot("http://10.6.193.204/ci");
            Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the html is "+html_string);
            instance.sendMail(from, to, cc, bcc, title, html_string, priority, bodyFormat, emailType, attachFilePath);
        }                     
    
    }
    
    /**
     * implement the trigger's Job class method 
     */
    public void execute(JobExecutionContext jec) throws JobExecutionException {
           sendToolsExcuteInforToDuty();  
    }
    
}
